package com.mango.ch01;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.io.WritableComparable;

/**
 * 定制数据类型，实现Hadoop的序列化的比较接口
 * 
 * @author 游客
 *
 */
public class DateTemperaturePair implements WritableComparable<DateTemperaturePair> {

	private Text yearMonth = new Text();// 自然建
	private Text day = new Text();
	private IntWritable temperature = new IntWritable();// 次键

	public Text getYearMonth() {
		return yearMonth;
	}

	public void setYearMonth(Text yearMonth) {
		this.yearMonth = yearMonth;
	}

	public Text getDay() {
		return day;
	}

	public void setDay(Text day) {
		this.day = day;
	}

	public IntWritable getTemperature() {
		return temperature;
	}

	public void setTemperature(IntWritable temperature) {
		this.temperature = temperature;
	}

	@Override
	public int compareTo(DateTemperaturePair pair) {
		// TODO Auto-generated method stub
		int compareValue = this.yearMonth.compareTo(pair.getYearMonth());// 自然键排序
		if (compareValue == 0) {
			compareValue = temperature.compareTo(pair.getTemperature());// 次键排序
		}
		return -1 * compareValue;// 降序
	}

	@Override
	public void write(DataOutput out) throws IOException {
		// TODO Auto-generated method stub
		this.yearMonth.write(out);
		this.day.write(out);
		this.temperature.write(out);
	}

	@Override
	public void readFields(DataInput in) throws IOException {
		// TODO Auto-generated method stub
		this.yearMonth.readFields(in);
		this.day.readFields(in);
		this.temperature.readFields(in);
	}

	@Override
	public String toString() {
		return "DateTemperaturePair [yearMonth=" + yearMonth + ", day=" + day + ", temperature=" + temperature + "]";
	}

}
